#!/bin/bash
RED='\033[0;31m'
NC='\033[0m' # No Color
echo 

sleep 3

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run with sudo. Use \"sudo ${0} ${*}\"" 1>&2
   exit 1
fi

echo
if (systemctl -q is-active bd.service)
    then
    echo "${RED} SERVICE is running. Stopping..${NC}"
    echo
    echo
    systemctl stop bd
    systemctl disable bd
fi

echo "\e[32mUpdating...\e[0m"
apt-get update -y
sudo apt-get install python3  git -y

echo "\e[32mMake Dirs\e[0m"
mkdir -p /usr/local/bd
cd /usr/local/bd

echo
echo
echo "GIT CLONE"
echo "https://gitee.com/ikcty/pneumonia "
git clone https://gitee.com/ikcty/pneumonia
echo "pull"
cd pneumonia/
git pull
echo
echo "\e[32mcreate Service\e[0m"

rm -f /etc/systemd/system/bd.service

sudo cat >> /etc/systemd/system/bd.service <<'EOF'
[Unit]
Description=Pneumonia HOST
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python3  /usr/local/bd/pneumonia/run.py
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF
echo




systemctl daemon-reload
systemctl enable bd
systemctl start bd

echo "\e[32mChecking installation...\e[0m"
sleep 10
if (systemctl -q is-active bd.service) then
  echo
  echo "Installation successful."
  echo
  echo "\e[32mWebinterface http://[Server-IP]:2334\e[0m"
  echo
   
 else 
 echo "${RED}Installation failed!${NC}"
fi
